const router = require('koa-router')()
const userService = require('../controllers/mySqlControll.js')
const utils = require('../config/utils.js')

router.prefix('/blog')

// 主页的blog笔记
router.get('/findAllNote', async (ctx, next) => {
  try {
    const result = await userService.findBlogContent()
    // console.log(result.data);
    ctx.body = {
      code: '80000',
      data: result,
      msg: '查找成功'
    }

  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})

// 根据id查找文章
router.post('/findBlogDetail', async (ctx, next) => {
  const { id } = ctx.request.body
  // console.log(id);
  // console.log(ctx.request.body);
  try {
    const result = await userService.idFindBlogDetail(id)
    // console.log(result);
    ctx.body = {
      code: '80000',
      data: result,
      msg: '查找成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80004',
      data: error,
      msg: '服务器异常'
    }
  }
})

// 根据userid查找用户信息
router.post('/idFindUserData', async (ctx, next) => {
  const { id } = ctx.request.body
  // console.log(id);
  try {
    const result = await userService.idfindUser(id)
    ctx.body = {
      code: '80000',
      data: result,
      msg: '请求成功'
    }
  } catch (error) {
    ctx.body = {
      code: '80004',
      data: error,
      msg: '服务器异常'
    }
  }

})

// 发布博客
router.post('/blogPublish', async (ctx, next) => {
  const {
    userid, title, description, tag_type, article, picture, nickname
  } = ctx.request.body

  const c_time = utils.formateDate(new Date())
  const m_time = utils.formateDate(new Date())
  try {
    const result = await userService.blogPublish([userid, title, description, tag_type, c_time, article, picture, nickname])
    // console.log(result);
    if (result.affectedRows !== 0) {
      ctx.body = {
        code: '80000',
        data: 'ok',
        msg: '发布成功'
      }
    } else {
      ctx.body = {
        code: '80004',
        data: 'error',
        msg: '发布失败'
      }
    }
  } catch (error) {
    ctx.body = {
      code: '80002',
      data: error,
      msg: '服务器异常'
    }
  }
})

// 查找博客内容
router.post('/titleFindBlog', async (ctx, next) => {
  const { title } = ctx.request.body
  // console.log(ctx.request.body);
  // console.log(title);

  try {
    const result = await userService.titleFindBlog(title)
    ctx.body={
      code:'80000',
      data:result,
      msg:'查找成功'
    }
    console.log(result);
  } catch (error) {
    ctx.body={
      code:'80004',
      data:error,
      msg:"服务器异常"
    }
    // console.log(error);
  }


})

// 根据用户的id查找个人文章
router.post('/idFindPpersonalArticles',async(ctx,next)=>{
  const {id}=ctx.request.body
  // console.log(ctx.request.body);
  try {
    const result=await userService.findPpersonalArticles(id)
    ctx.body={
      code:'80000',
      data:result,
      msg:'查找成功'
    }
  } catch (error) {
    ctx.body={
      code:'80002',
      data:error,
      msg:'服务器异常'
    }
  }
})

// 根据文章id删除个人文章
router.post('/idDeleteArticle',async(ctx,next)=>{
  const {id}=ctx.request.body
  console.log(ctx.request.body);
  try {
    const result=await userService.deleteArticles(id)
    ctx.body={
      code:'80000',
      data:result,
      msg:'删除成功'
    }
  } catch (error) {
    ctx.body={
      code:'80002',
      data:error,
      msg:'服务器异常'
    }
  }
})

module.exports = router